package com.hans.myscanner.Scanner.Activity;


import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.hans.dialog.TransBgDialog;
import com.hans.myscanner.R;
import com.hans.myscanner.entity.ContactEntity;

public class MakeSubActivity extends AppCompatActivity{

    private View makeContentBtn,makeUrlBtn,makeTextBtn;
    private Dialog makeContactDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_make);
        findView();
        initView();
    }

    protected void findView() {
        makeContentBtn=findViewById(R.id.make_contact);
        makeUrlBtn=findViewById(R.id.make_url);
        makeTextBtn=findViewById(R.id.make_text);

    }
//
//    @Override
    protected void initView() {
        makeContentBtn.setOnClickListener(onClickListener);
        makeUrlBtn.setOnClickListener(onClickListener);
        makeTextBtn.setOnClickListener(onClickListener);
        initDialog();

    }
    protected  void initDialog(){
        makeContactDialog=new TransBgDialog(MakeSubActivity.this,R.layout.make_contact_dialog);
        makeContactDialog.findViewById(R.id.choose_contact_btn).setOnClickListener(onClickListener);
        makeContactDialog.findViewById(R.id.new_contact_btn).setOnClickListener(onClickListener);
        makeContactDialog.findViewById(R.id.close_btn).setOnClickListener(onClickListener);

    }

    private View.OnClickListener onClickListener=new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.make_contact:
                    makeContactDialog.show();
                    break;
                case R.id.make_url:

                    break;
                case R.id.make_text:

                    break;
                case R.id.choose_contact_btn:
                    jumpToContactActivity();
                    break;
                case R.id.new_contact_btn:

                    break;
                case  R.id.close_btn:
                    makeContactDialog.dismiss();
                    break;

            }
        }
    };

    private int REQUEST_CODE_CHOOSE_CONTACT=10;
    private void jumpToContactActivity(){
        Intent intent=new Intent();
        intent.setAction(Intent.ACTION_PICK);
        intent.setData(ContactsContract.Contacts.CONTENT_URI);
        startActivityForResult(intent,REQUEST_CODE_CHOOSE_CONTACT);
    }
//
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode != REQUEST_CODE_CHOOSE_CONTACT || resultCode != RESULT_OK) return;
        Uri uri = data.getData();
        ContactEntity contactEntity = new ContactEntity();
        Cursor cursor = getContentResolver().query(uri, null, null, null, null);
        if (cursor == null) return;
        String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
        String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
        cursor.close();
        contactEntity.setName(name);
        Cursor phoneCur = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null);
        while (phoneCur != null && phoneCur.moveToNext()) {
            String phone = phoneCur.getString(phoneCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            contactEntity.getPhoneNums().add(phone);
        }
        phoneCur.close();
        Cursor emailCur = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
                ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId, null, null);
        while (emailCur != null && emailCur.moveToNext()) {
            String email = emailCur.getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
            contactEntity.getEmails().add(email);
        }
        emailCur.close();
        String whereStr = ContactsContract.Data.CONTACT_ID + " = " + contactId + " AND " + ContactsContract.Data.MIMETYPE
                + " = " + ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE;
        Cursor addressCur = getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, whereStr, null, null);
        if (addressCur != null && addressCur.moveToFirst()) {
            contactEntity.setAddress(addressCur.getString(addressCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET)));
        }
        addressCur.close();


        Intent intent = new Intent(MakeSubActivity.this, EncodeContactActivity.class);
        intent.putExtra(EncodeContactActivity.KEY_CONTACT_ENTITY,contactEntity);
//        intent.putExtra(EncodeContactActivity.KEY_CONTACT_ENTITY, contactEntity);
        startActivity(intent);
    }


}
